<template>
  <div id="home">
    <nav-bar class="nav-bar"><div slot="center">购物商城</div></nav-bar>
     <tab-control ref="tabControl1"  :titles="['流行','新款','精选']"
           @tabClick="tabClick" class="tabcontrol" v-show="isTabFixed"></tab-control>
    <scroll class="content" ref="scroll" :probe-type="3"
     @scroll="contentScroll" :pull-up-load="true" @pullingUp="loadMore">
        <home-swiper :banners="banners" @swiperImageLoad="swiperImageLoad"/>
        <recommend-view :recommends="recommends" />
          <feature-view />
          <tab-control ref="tabControl2"  :titles="['流行','新款','精选']"
           @tabClick="tabClick"></tab-control>
        <goods-list :goodsList="showGoods"/>
    </scroll> 
    <back-top @click.native="backClick" v-show="isShowBackTop"/>
   
  </div>
</template>

<script>
import NavBar from 'common/navbar/NavBar'
import HomeSwiper from './childComps/HomeSwiper'
import RecommendView from './childComps/RecommendView'
import FeatureView from './childComps/feature'
import TabControl from 'content/tabControl/TabControl'
import GoodsList from 'content/goods/GoodsList'
import Scroll from 'common/scroll/Scroll'



import {getHomeMultidata,getHomeData} from "network/home"
import {debounce} from "../../common/utils"
import {itemListenerMinxin,backTopMixin} from "../../common/mixin"


export default {
  name:'Home',
   mixins:[itemListenerMinxin,backTopMixin],
   data () {
      return {
        banners:[],
        recommends:[],
        goodsList:{
          'pop':{page:1,list:[]},
          'new':{page:1,list:[]},
          'sell':{page:1,list:[]},
        },
        currentType:'pop',
        tabOffsetTop:0,
        isTabFixed:false,
        saveY:0,
        
        
      }
   },

   components: {
     NavBar,
     HomeSwiper,
     RecommendView,
     FeatureView,
     TabControl,
     GoodsList,
     Scroll,
    
   },
   created(){
    //1.请求多个数据
    this.getMultiData()

    // 2.请求商品数据
      this.getHomeData('pop')
      this.getHomeData('new')
      this.getHomeData('sell')

  
   },
   mounted(){
  
   },

   computed: {
     showGoods(){
       return this.goodsList[this.currentType].list
     }
   },
   activated(){
     this.$refs.scroll.scrollTo(0,this.saveY,0)  
     this.$refs.scroll.refresh()
   },
   deactivated(){
     //1.保存Y值
     this.saveY = this.$refs.scroll.getScrollY()
     //2.取消全局事件的监听
     this.$bus.$off('itemImgLoad',this.itemImgListener)
   },

   methods: {
     //事件监听相关
     tabClick(index){
       console.log(index)
      switch(index){
        case 0:
          this.currentType = 'pop'
          break
        case 1:
          this.currentType = 'new'
          break
        case 2:
          this.currentType = 'sell'
          break
      }
      this.$refs.tabControl1.currentIndex = index;
      this.$refs.tabControl2.currentIndex = index;

     },
     contentScroll(position){
       //判断backtop是否显示
      this.isShowBackTop = (-position.y ) > 1000
      //判断tabcontrol是否吸顶
      this.isTabFixed = (-position.y) > this.tabOffsetTop

     },
     loadMore(){
       this.getHomeData(this.currentType)
     },
     swiperImageLoad(){
       //2.获取tabControl的offsetTop
      //所有的组件都有一个属性$el:用于获取组件中的元素
        this.tabOffsetTop = this.$refs.tabControl2.$el.offsetTop
     },









    //  网络请求相关方法
    getMultiData(){
      getHomeMultidata().then(res => {
        this.banners = res.data.banner.list;
        this.recommends = res.data.recommend.list;
      })
    },
    getHomeData(type){
      const page = this.goodsList[type].page + 1
      getHomeData(type,page).then(res => {
        const goodsList = res.data.list;
        this.goodsList[type].list.push(...goodsList)
        this.goodsList[type].page +=1
        this.$refs.scroll.finishPullUp()
      })
    }
   

   }
}
</script>
<style  scoped>
 #home {
    /*position: relative;*/
    height: 100vh;
  }
  .nav-bar{
    background-color: var(--color-tint);
    color: #fff;
  }
  .tabcontrol{
    position: relative;
    z-index: 9;
  }
  
  .content{
    /* 方法一 */
    /* height: calc(100% - 93px);
    overflow: hidden; */

    /* 方法二 */
    position: absolute;
    top: 44px;
    bottom: 49px;
    left: 0;
    right: 0;
   
  }
</style>
